import { getData } from '@vimi/utils-api'
import { add } from '@vimi/utils-tool'

const getFirstData = res => {
  return (res && res.data && res.data.length && res.data[0]) || {}
}

const state = {
  taskCountLimit: {},
  taskCountLimitNew: {},
  taskCountIndustry: {},
  taskCountIndustryNew: {},
  taskCountSimple: {},
  taskCountNormal: {},
  taskCounthpLimit: {},
}

const getters = {
  taskCountLimit: state => state.taskCountLimit,
  taskCountIndustryNew: state => state.taskCountIndustryNew,
  taskCountSimple: state => state.taskCountSimple,
  taskCountIndustry: state => state.taskCountIndustry,
  taskCountNormal: state => state.taskCountNormal,
  taskCountLimitNew: state => state.taskCountLimitNew,
  taskCounthpLimit: state => state.taskCounthpLimit,
}

// this.$store.dispatch('...')
const actions = {
  getTaskCountLimit({ commit, rootGetters }, { type = 0, id = '' } = {}) {
    return getData('credit', 'to_do_list_xl_view', {
      'project_type_id.eq': id,
      'bank_user_id.eq': rootGetters.userId,
      'channel_type.eq': type,
    }).then(res => {
      const sumData = res?.data ?? []
      let data = {}
      if (id) {
        data = getFirstData(res)
        commit('taskCountLimit', data)
      } else {
        data = sumData.reduce((a, item) => {
          a.todo_apply_total = add(a.todo_apply_total, item.todo_apply_total)
          a.todo_confirm_total = add(a.todo_confirm_total, item.todo_confirm_total)
          a.todo_datum_total = add(a.todo_datum_total, item.todo_datum_total)
          a.todo_exc_total = add(a.todo_exc_total, item.todo_exc_total)
          a.todo_exp_total = add(a.todo_exp_total, item.todo_exp_total)

          return a
        }, {})
        commit('taskCountLimit', data)
      }
      return data
    })
  },
  getTaskCountLimitNew({ commit, rootGetters }, { id = '' } = {}) {
    return getData('credit', 'to_do_list_xl_view_zl', {
      'project_type_id.eq': id,
      'bank_user_id.eq': rootGetters.userId,
    }).then(res => {
      const sumData = res?.data ?? []
      let data = {}
      if (id) {
        data = getFirstData(res)
        commit('taskCountLimitNew', data)
      } else {
        data = sumData.reduce((a, item) => {
          a.todo_apply_total = add(a.todo_apply_total, item.todo_apply_total)
          a.todo_confirm_total = add(a.todo_confirm_total, item.todo_confirm_total)
          a.todo_datum_total = add(a.todo_datum_total, item.todo_datum_total)
          a.todo_exc_total = add(a.todo_exc_total, item.todo_exc_total)
          a.todo_exp_total = add(a.todo_exp_total, item.todo_exp_total)

          return a
        }, {})
        commit('taskCountLimitNew', data)
      }
      return data
    })
  },
  getTaskCountIndustry({ commit, rootGetters }, { id = '' } = {}) {
    return getData('credit', 'to_do_list_xl_view_rural', {
      'project_type_id.eq': id,
      'bank_user_id.eq': rootGetters.userId,
    }).then(res => {
      const sumData = res?.data ?? []
      let data = {}
      if (id) {
        data = getFirstData(res)
        commit('taskCountIndustry', data)
      } else {
        data = sumData.reduce((a, item) => {
          a.todo_apply_total = add(a.todo_apply_total, item.todo_apply_total)
          a.todo_confirm_total = add(a.todo_confirm_total, item.todo_confirm_total)
          a.todo_datum_total = add(a.todo_datum_total, item.todo_datum_total)
          a.todo_exc_total = add(a.todo_exc_total, item.todo_exc_total)
          a.todo_exp_total = add(a.todo_exp_total, item.todo_exp_total)

          return a
        }, {})
        commit('taskCountIndustry', data)
      }
      return data
    })
  },
  getTaskCountIndustryNew({ commit, rootGetters }, { id = '' } = {}) {
    return getData('credit', 'to_do_list_xl_view_zl_rural', {
      'project_type_id.eq': id,
      'bank_user_id.eq': rootGetters.userId,
    }).then(res => {
      const sumData = res?.data ?? []
      let data = {}
      if (id) {
        data = getFirstData(res)
        commit('taskCountIndustryNew', data)
      } else {
        data = sumData.reduce((a, item) => {
          a.todo_apply_total = add(a.todo_apply_total, item.todo_apply_total)
          a.todo_confirm_total = add(a.todo_confirm_total, item.todo_confirm_total)
          a.todo_datum_total = add(a.todo_datum_total, item.todo_datum_total)
          a.todo_exc_total = add(a.todo_exc_total, item.todo_exc_total)
          a.todo_exp_total = add(a.todo_exp_total, item.todo_exp_total)

          return a
        }, {})
        commit('taskCountIndustryNew', data)
      }
      return data
    })
  },
  getTaskCountSimple({ commit, rootGetters }, id = '') {
    return getData('credit', 'to_do_list_28_quick_view', {
      'project_type_id.eq': id,
      'bank_user_id.eq': rootGetters.userId,
    }).then(res => {
      const sumData = res?.data ?? []
      let data = {}
      if (id) {
        data = getFirstData(res)
        commit('taskCountSimple', data)
      } else {
        data = sumData.reduce((a, item) => {
          a.todo_claim_total = add(a.todo_claim_total, item.todo_claim_total)
          a.todo_datum_total = add(a.todo_datum_total, item.todo_datum_total)
          a.todo_recommend_total = add(a.todo_recommend_total, item.todo_recommend_total)
          a.todo_approval_register_total = add(
            a.todo_approval_register_total,
            item.todo_approval_register_total
          )
          a.todo_exp_total = add(a.todo_exp_total, item.todo_exp_total)
          return a
        }, {})
        commit('taskCountSimple', data)
      }
      return data
    })
  },
  getTaskCountNormal({ commit, rootGetters }, id = '') {
    return getData('credit', 'to_do_list_28_view', {
      'project_type_id.eq': id,
      'bank_user_id.eq': rootGetters.userId,
    }).then(res => {
      const sumData = res?.data ?? []
      let data = {}
      if (id) {
        data = getFirstData(res)
        commit('taskCountNormal', data)
      } else {
        data = sumData.reduce((a, item) => {
          a.todo_claim_total = add(a.todo_claim_total, item.todo_claim_total)
          a.todo_approval_register_total = add(
            a.todo_approval_register_total,
            item.todo_approval_register_total
          )
          a.todo_loan_register_total = add(
            a.todo_loan_register_total,
            item.todo_loan_register_total
          )
          a.todo_exp_total = add(a.todo_exp_total, item.todo_exp_total)
          return a
        }, {})
        commit('taskCountNormal', data)
      }
      return data
    })
  },
  getTaskCounthpLimit({ commit, rootGetters }) {
    return getData('credit', 'to_do_list_xl_view_zl_ph', {
      'bank_user_id.eq': rootGetters.userId,
    }).then(res => {
      const data = getFirstData(res)
      commit('taskCounthpLimit', data)
    })
  },
}

const mutations = {
  taskCountLimit: (state, payload) => (state.taskCountLimit = payload),
  taskCountIndustry: (state, payload) => (state.taskCountIndustry = payload),
  taskCountIndustryNew: (state, payload) => (state.taskCountIndustryNew = payload),
  taskCountSimple: (state, payload) => (state.taskCountSimple = payload),
  taskCountNormal: (state, payload) => (state.taskCountNormal = payload),
  taskCountLimitNew: (state, payload) => (state.taskCountLimitNew = payload),
  taskCounthpLimit: (state, payload) => (state.taskCounthpLimit = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
